﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook
{
    public partial class News : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (Request.QueryString["groupID"] == null)
                if(Page.RouteData.Values["groupID"]==null)
                {
                    BindNewsTop10();
                    lblTitleTop.Text = "Tin mới nhất";
                }
                else
                {
                    //string groupId = Request.QueryString["groupID"];
                    string groupId = Page.RouteData.Values["groupID"].ToString();
                    BindNews_ByGroupId(groupId);
                    lblTitleTop.Text = "Tin " + Business.NewsGroupService.NewsGroup_GetById(groupId)[0].GroupName;
                }
               
            }
        }

       

        private void BindNewsTop10()
        {
            clPager.PageSize = 3; // số sản phẩm mỗi trang
            clPager.DataSource = Business.NewsService.News_GetByTop("10","NStatus=1","NDate desc"); // lấy dữ liệu từ database
            clPager.BindToControl = dtlNews; // có thể dùng cho repeater hoặc datalist..
            dtlNews.DataSource = clPager.DataSourcePaged;

            //dtlNews.DataSource = Business.NewsService.News_GetTop10_ByStatusTrue();
            //dtlNews.DataBind();
        }

        private void BindNews_ByGroupId(string groupID)
        {
            try
            {
                clPager.PageSize = 3; // số sản phẩm mỗi trang
                clPager.DataSource = Business.NewsService.News_GetByTop("", "GroupID=" + groupID + " and NStatus=1", "NDate desc");  // lấy dữ liệu từ database
                clPager.BindToControl = dtlNews; // có thể dùng cho repeater hoặc datalist..
                dtlNews.DataSource = clPager.DataSourcePaged;
            }
            //dtlNews.DataSource = Business.NewsService.News_GetByGroupId_StatusTrue(groupID);
            //dtlNews.DataBind();
            catch{}
        }

        public string GetTitleById(object Id)
        {
            string title = Business.NewsService.News_GetById(Id.ToString())[0].nTitle.ToString();
            return title.Replace("\"","").Replace(" ","");
        }

        public string GetGroupNameById(object Id)
        {
            string groupName = Business.NewsGroupService.NewsGroup_GetById(Id.ToString())[0].GroupName;
            return groupName.Replace(" ","");
        }
    }
}